<?php
require_once 'vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

// 模拟真实的API响应数据
$mockShopResponse = [
    "msg" => "",
    "result" => [
        "TotalNum" => 3,
        "Offset" => 0,
        "PageSize" => 100,
        "PageTotalNum" => 0,
        "PageIndex" => 1,
        "Data" => [
            [
                "MallName" => "苍南县爱哭森优品小铺",
                "AuthId" => 110699,
                "AppName" => "晓风一键下单(抖音)",
                "PlatformType" => 3,
                "PlatformTypeStr" => "抖音",
                "ExpiresTime" => "2025-08-07 08:40:49",
                "AppExpiresTime" => "2025-08-14 15:42:36",
                "Id" => 112333,
                "MemberId" => 85177,
                "MallId" => "222842442",
                "AppId" => 9,
                "CreateTime" => "2025-05-05 23:36:30"
            ],
            [
                "MallName" => "苍南县爱玩森甄选店",
                "AuthId" => 125451,
                "AppName" => "晓风一键下单(抖音)",
                "PlatformType" => 3,
                "PlatformTypeStr" => "抖音",
                "ExpiresTime" => "2025-08-11 11:52:18",
                "AppExpiresTime" => "2025-09-23 10:30:00",
                "Id" => 127141,
                "MemberId" => 85177,
                "MallId" => "231105629",
                "AppId" => 9,
                "CreateTime" => "2025-06-23 10:30:49"
            ],
            [
                "MallName" => "顽皮森优品小铺",
                "AuthId" => 131505,
                "AppName" => "晓风一键下单(抖音)",
                "PlatformType" => 3,
                "PlatformTypeStr" => "抖音",
                "ExpiresTime" => "2025-08-06 20:59:45",
                "AppExpiresTime" => "2025-10-09 19:11:17",
                "Id" => 133025,
                "MemberId" => 85177,
                "MallId" => "234883301",
                "AppId" => 9,
                "CreateTime" => "2025-07-09 19:11:55"
            ]
        ],
        "Datas" => null,
        "Orders" => ["-Id"],
        "ExtData" => ""
    ],
    "success" => true
];

$mockOrderResponse = [
    "msg" => "",
    "result" => [
        "TotalNum" => 1,
        "Offset" => 0,
        "PageSize" => 100,
        "PageTotalNum" => 0,
        "PageIndex" => 0,
        "Data" => [
            [
                "OrderInfo" => [
                    "OrderId" => "6920510668436569981",
                    "CreateTime" => "2025-07-28 17:23:49",
                    "OrderStatus" => 3,
                    "OrderAmount" => 1749,
                    "PayAmount" => 1449
                ],
                "ItemInfos" => [
                    [
                        "GoodsName" => "2025新款春鸭舌帽大脸显脸小宽帽檐大围加大加深斜杠刺绣棒球帽",
                        "GoodsCount" => 1,
                        "GoodsPrice" => 1749,
                        "TotalPrice" => 1749
                    ]
                ],
                "BuyerInfo" => [
                    "Name" => "韩*"
                ]
            ]
        ],
        "Datas" => null,
        "Orders" => ["-Id"],
        "ExtData" => ""
    ],
    "success" => true
];

echo "测试API响应解析...\n";

// 测试店铺数据解析
echo "\n=== 测试店铺数据解析 ===\n";
$shopIds = [];
if (isset($mockShopResponse['result']['Data']) && is_array($mockShopResponse['result']['Data'])) {
    foreach ($mockShopResponse['result']['Data'] as $shop) {
        if (isset($shop['Id'])) {
            $shopIds[] = $shop['Id'];
            echo "店铺ID: {$shop['Id']}, 店铺名称: {$shop['MallName']}\n";
        }
    }
}

echo "提取的店铺ID: " . implode(', ', $shopIds) . "\n";

// 测试订单数据解析
echo "\n=== 测试订单数据解析 ===\n";
if (isset($mockOrderResponse['result']['Data']) && is_array($mockOrderResponse['result']['Data'])) {
    foreach ($mockOrderResponse['result']['Data'] as $orderData) {
        $orderInfo = $orderData['OrderInfo'] ?? [];
        $itemInfos = $orderData['ItemInfos'] ?? [];
        $buyerInfo = $orderData['BuyerInfo'] ?? [];
        
        if (!empty($itemInfos)) {
            $item = $itemInfos[0];
            $orderNo = $orderInfo['OrderId'] ?? '';
            $customerName = $buyerInfo['Name'] ?? '';
            $productName = $item['GoodsName'] ?? '';
            $quantity = $item['GoodsCount'] ?? 1;
            $unitPrice = ($item['GoodsPrice'] ?? 0) / 100; // 转换为元
            $totalAmount = ($item['TotalPrice'] ?? 0) / 100; // 转换为元
            
            echo "订单号: $orderNo\n";
            echo "客户姓名: $customerName\n";
            echo "商品名称: $productName\n";
            echo "数量: $quantity\n";
            echo "单价: $unitPrice 元\n";
            echo "总金额: $totalAmount 元\n";
            echo "订单状态: " . ($orderInfo['OrderStatus'] ?? '未知') . "\n";
        }
    }
}

echo "\n=== 测试完成 ===\n"; 